package com.dlwx.firefighting.fragment.trade;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dlwx.firefighting.R;
import com.dlwx.firefighting.adapter.OnlineAdapter;
import com.dlwx.firefighting.bean.OnlineBean;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.utils.SPUtils;
import com.dlwx.firefighting.utils.ToastUtils;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 用户订单fragment
 */
public class ThreeFragment extends Fragment {

    @BindView(R.id.transferlist_recycler)
    LRecyclerView recyclerView;
    Unbinder unbinder;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private OnlineAdapter mAdapter;

    private int page = 1;
    private int requestNum = 10;
    private int count = 0;
    private String userId;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_trade_three, container, false);
        unbinder = ButterKnife.bind(this, view);
        userId = (String) SPUtils.get(getActivity(), Constant.USER_ID, "");
        initRecyView();
        Bundle bundle = getArguments();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        refresh();
    }

    private void getDatas() {
        OkGo.<String>post(Constant.ONLINE_List)
                .tag(this)
                .params("user_id", userId)
                .params("page", page)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
//                        DealResult dealResult = new DealResult();
//                        OnlineBean bean = dealResult.dealOne(getActivity(), response,new TypeToken<BaseOneBean<OnlineBean>>(){}.getType());
//                        if(bean==null){
//                            return;
//                        }
//                            setDatas(bean);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        ToastUtils.toastForShort(getActivity(), getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }

    private void setDatas(OnlineBean bean) {
       /* if(beans.isEmpty()){
            return;
        }*/
        mAdapter.addAll(bean.getOnline_log());
        recyclerView.refreshComplete(requestNum);
    }

    /**
     * 初始化recyclerview
     */
    private void initRecyView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new OnlineAdapter(getActivity());
        lRecyclerViewAdapter = new LRecyclerViewAdapter(mAdapter);
        recyclerView.setAdapter(lRecyclerViewAdapter);

        DividerDecoration divider = new DividerDecoration.Builder(getActivity())
                .setHeight(R.dimen.divider_height)
                .setPadding(R.dimen.divider_padding)
                .setColorResource(R.color.line_gray)
                .build();

        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(divider);

        recyclerView.setRefreshProgressStyle(ProgressStyle.LineSpinFadeLoader);
        recyclerView.setArrowImageView(R.drawable.ic_pulltorefresh_arrow);
        recyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);

        //设置下拉刷新
        recyclerView.setPullRefreshEnabled(true);
        recyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();

            }
        });
        //设置上拉加载更多
        recyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                if (count < (page * requestNum)) {
                    ToastUtils.toastForShort(getActivity(), getString(R.string.no_more));
                    recyclerView.setNoMore(true);
                } else {
                    page++;
                    getDatas();

                }
            }
        });
        //设置头部加载颜色
        recyclerView.setHeaderViewColor(R.color.colorAccent, R.color.black, android.R.color.white);
        //设置底部加载颜色
        recyclerView.setFooterViewColor(R.color.colorAccent, R.color.black, android.R.color.white);
        //设置底部加载文字提示
        recyclerView.setFooterViewHint("拼命加载中", "已经全部为你呈现了", "网络不给力啊，点击再试一次吧");
        //初始化数据

    }

    /**
     * 刷新
     */
    private void refresh() {
        mAdapter.clear();
        lRecyclerViewAdapter.notifyDataSetChanged();
        page = 1;
        getDatas();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    //-------------------------------------------------------------
}
